﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone;
using Microsoft.Phone.Tasks;
using System.Xml.Serialization;
using goodreads.Common;

namespace goodreads
{
    public partial class MainPage : PhoneApplicationPage
    {
        GoodreadsAccess gruser = Helper.LoadSetting<GoodreadsAccess>(Constants.GoodreadsAccess);

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        void goodreads_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

            WebClient goodreads = new WebClient();

            goodreads.DownloadStringCompleted += new DownloadStringCompletedEventHandler(goodreads_DownloadStringCompleted);
            goodreads.DownloadStringAsync(new Uri("http://goodreads.com/user/show/1049144.xml?key=by4NQMOC8fercdZW7spixA"));
            
            if (e.Error != null) return;

//            XElement xmlUpdates = XElement.Parse(e.Result);

           // string image = xmlUpdates.Element("status").Element("user").Element("profile_image_url").Value;
           // ImageSource source = new BitmapImage(new Uri(image));
           // TwitterImage.Source = source;

  //          string name = xmlUpdates.Element("status").Element("user").Element("screen_name").Value;


        }

        private void MenuItemSignIn_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AuthPage.xaml", UriKind.Relative));
        }

        private void ResetMenuItems()
        {
            Boolean signInEnabled = true;
            Boolean signOutEnabled = false;
            if (!gruser.AccessToken.Equals(string.Empty))
            {
                signInEnabled = false;
                signOutEnabled = true;
            }

            Dispatcher.BeginInvoke(() =>
            {
                var SignInMenuItem = (Microsoft.Phone.Shell.ApplicationBarMenuItem)this.ApplicationBar.MenuItems[0];
                SignInMenuItem.IsEnabled = signInEnabled;

                var SignOutMenuItem = (Microsoft.Phone.Shell.ApplicationBarMenuItem)this.ApplicationBar.MenuItems[1];
                SignOutMenuItem.IsEnabled = signOutEnabled;
            });
        }

        private void MenuItemSignOut_Click(object sender, EventArgs e)
        {
            gruser.AccessToken = string.Empty;
            gruser.AccessTokenSecret = string.Empty;
            Helper.SaveSetting(Constants.GoodreadsAccess, gruser);
            ResetMenuItems();
        }


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ResetMenuItems();
        }
    }
}